package com.show.service.sensor;

import com.show.dao.population.PopulationRepository;
import com.show.dao.sensor.GasSensorRepository;
import com.show.dto.Histogram;
import com.show.dto.sensor.GasSensor;
import com.show.dto.unit.UnitType;
import com.show.util.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 有害气体传感器service
 *
 * @author 姜文
 */
@Service
public class GasSensorService {

    private QueryUtil queryUtil;

    private GasSensorRepository gasSensorRepository;

    private PopulationRepository populationRepository;

    @Autowired
    public GasSensorService(QueryUtil queryUtil, GasSensorRepository gasSensorRepository, PopulationRepository populationRepository) {
        this.queryUtil = queryUtil;
        this.gasSensorRepository = gasSensorRepository;
        this.populationRepository = populationRepository;
    }


    /**
     * 有害气体传感器总数
     *
     * @author 姜文
     */
    public List<GasSensor> gasSensorTotal() {
        String sql = "SELECT * FROM `gas_sensor`";
        List<GasSensor> gasSensorList = queryUtil.query(sql, GasSensor.class);
        return gasSensorList;
    }

    /**
     * 气体传感器总数
     *
     * @return long 数量
     */
    public long countAll() {
        return gasSensorRepository.count();
    }


    /**
     * 井室有害气体监测柱状图
     *
     * @return List
     */
    public List<Histogram> gasSensorMonitor() {
        List<Histogram> histogramList = new ArrayList<Histogram>();
        List<UnitType> unitTypeList = queryUnitTypeList();
        for (UnitType unitType : unitTypeList) {
            int errorCount = queryErrorCount(unitType.getUnitTypeId());
            int shaftCount = queryShaftCount(unitType.getUnitTypeId());
            int workCount = shaftCount - errorCount;
            Histogram histogram = new Histogram(workCount, errorCount, unitType.getUnitTypeName());
            histogramList.add(histogram);
        }
        return histogramList;

    }

    private int queryShaftCount(String unitTypeId) {
        String sql = "select * from gas_sensor gas where gas.signalQuality = " + unitTypeId + "";
        return queryUtil.query(sql, GasSensor.class).size();
    }

    private int queryErrorCount(String unitTypeId) {
        String sql = "select *\n" +
                "from gas_sensor_error error\n" +
                "  left join gas_sensor gas on gas.gas_sensor_id = error.gas_sensor_id\n" +
                "where gas.signalQuality = " + unitTypeId + "\n" +
                "group by error.gas_sensor_id\n";
        return queryUtil.query(sql, GasSensor.class).size();
    }

    /**
     * 获取全部井盖类型
     *
     * @return List
     */
    private List<UnitType> queryUnitTypeList() {
        String sql = "select\n" +
                "  unitType.unit_type_id   unitTypeId,\n" +
                "  unitType.unit_type_name unitTypeName\n" +
                "from unit_type unitType\n" +
                "where sts = '0'";
        return queryUtil.query(sql, UnitType.class);

    }


}
